﻿/*
 * Copyright (c) 2015-2016 Constantijn Evenhuis (blueprints@const.nl)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
using Blueprints.IO;
using System;
using System.IO;
using System.Text;
using System.Xml;

namespace Blueprints.Xml
{
    /// <summary>
    /// A blueprint writer that writes xml.
    /// </summary>
    public sealed class XmlBlueprintWriter : IBlueprintWriter
    {
        internal const string BlueprintNamespace = "http://const.nl/blueprints";
        private const string BlueprintPrefix = "blueprints";
        private readonly Stream stream;
        private readonly XmlWriter writer;
        private bool blueprintNamespaceWritten;

        /// <summary>
        /// Creates a new XmlBlueprintWriter.
        /// </summary>
        /// <param name="stream">The stream to write to.</param>
        /// <param name="documentUri">The uri of the blueprint being written to.</param>
        public XmlBlueprintWriter(Stream stream, Uri documentUri)
        {
            XmlWriterSettings settings = new XmlWriterSettings()
            {
                ConformanceLevel = ConformanceLevel.Document,
                Encoding = Encoding.UTF8,
                Indent = true,
            };

            this.stream = stream;
            writer = XmlWriter.Create(stream, settings);
            DocumentUri = documentUri;
        }

        /// <summary>
        /// Gets the uri of the blueprint being written to.
        /// </summary>
        public Uri DocumentUri
        {
            get;
            private set;
        }

        /// <summary>
        /// Writes a node.
        /// </summary>
        /// <param name="node">The node to write.</param>
        public void Write(INode node)
        {
            switch (node.NodeType)
            {
                case NodeType.StartNode:
                    WriteStartNode(node.Name, node.Id, node.Type, node.Link);
                    break;
                case NodeType.Content:
                    WriteContentNode(node.Content);
                    break;
                case NodeType.EndNode:
                    WriteEndNode();
                    break;
                case NodeType.Other:
                    CommentNode commentNode = node as CommentNode;
                    if (commentNode != null)
                    {
                        WriteCommentNode(node.Content);
                    }

                    break;
            }
        }

        /// <summary>
        /// Disposes of the blueprint writer, releasing all used resources.
        /// </summary>
        public void Dispose()
        {
            writer.Dispose();
        }

        private void WriteStartNode(string name, string id, string type, string link)
        {
            writer.WriteStartElement(name);
            if (!blueprintNamespaceWritten)
            {
                writer.WriteAttributeString("xmlns", BlueprintPrefix, null, BlueprintNamespace);
                blueprintNamespaceWritten = true;
            }

            if (!string.IsNullOrEmpty(id))
            {
                writer.WriteAttributeString("id", id);
            }

            if (!string.IsNullOrEmpty(type))
            {
                writer.WriteAttributeString(BlueprintPrefix, "type", BlueprintNamespace, type);
            }

            if (!string.IsNullOrEmpty(link))
            {
                writer.WriteAttributeString(BlueprintPrefix, "link", BlueprintNamespace, link);
            }
        }

        private void WriteContentNode(string content)
        {
            writer.WriteValue(content);
        }

        private void WriteEndNode()
        {
            writer.WriteEndElement();
        }

        private void WriteCommentNode(string content)
        {
            writer.WriteComment(content);
        }
    }
}